package com.mhc.haval.service.impl;

import com.mhc.haval.api.dto.StaffDTO;
import com.mhc.haval.api.dto.StaffRoleDTO;
import com.mhc.haval.api.service.IStaffRoleService;
import com.mhc.haval.constant.MsgCodeEnum;
import com.mhc.haval.constant.RoleStatusEnum;
import com.mhc.haval.dal.mapper.ext.StaffRoleExtMapper;
import com.mhc.haval.dal.model.RoleDO;
import com.mhc.haval.dal.model.StaffDO;
import com.mhc.haval.dal.query.RoleQuery;
import com.mhc.haval.dal.query.StaffQuery;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.haval.manager.RoleManager;
import com.mhc.haval.manager.StaffManager;
import com.mhc.haval.vo.StaffRoleVO;
import com.subaru.common.util.BeanCopierUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HMJ on 2017/8/31.
 */
@Slf4j
@RestController
public class StaffRoleServiceImpl implements IStaffRoleService {

    @Autowired
    private StaffRoleExtMapper staffRoleExtMapper;

    @Autowired
    private StaffManager staffManager;

    @Autowired
    private RoleManager roleManager;


    /**
     * 根据角色code查找用户
     *
     * @param roleCode
     * @return
     * @throws Exception
     */
    @Override
    public List<StaffRoleDTO> selectStaffByRole(String roleCode) throws HavalCommonException{
        log.info("当前角色为：" + roleCode + ",开始查询该角色下的所属用户...");
        /**通过角色查询该角色下的所属用户**/
        List<StaffRoleVO> staffList = staffRoleExtMapper.queryStaffByRole(roleCode);
        List<StaffRoleDTO> staffDtoList = new ArrayList<>();
        for (StaffRoleVO staffRoleVO : staffList) {
            StaffRoleDTO staffRoleDTO = new StaffRoleDTO();
            BeanUtils.copyProperties(staffRoleVO, staffRoleDTO);
            staffDtoList.add(staffRoleDTO);
        }
        log.info("查询成功，当前角色下的所属用户为：" + staffDtoList);
        return staffDtoList;
    }

    /**
     * 根据员工id查询员工信息
     *
     * @param staffId
     * @return
     */
    @Override
    public StaffDTO selectByStaffName(Long staffId) throws HavalCommonException{
        log.info("开始查询当前用户ID为：" + staffId + "的资料");
        /**通过用户id查询用户名称**/
        StaffDO staffDO = staffManager.selectByPrimaryKey(staffId);
        StaffDTO staffDTO = new StaffDTO();
        if(staffDO!=null){
            BeanUtils.copyProperties(staffDO, staffDTO);
        }
        log.info("查询完成");
        return staffDTO;
    }

    /**
     * 根据部门id查询员工列表
     *
     * @param deptId
     * @return
     */
    @Override
    public List<StaffDTO> getStaffByDeptId(Long deptId) throws HavalCommonException {
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.createCriteria().andDeptIdEqualTo(deptId);
        List<StaffDTO> staffDTOS = new ArrayList<>();
        for (StaffDO staffDO : staffManager.selectByQuery(staffQuery)) {
            staffDTOS.add(BeanCopierUtil.convert(staffDO, StaffDTO.class));
        }
        if (!staffDTOS.isEmpty()) {
            return staffDTOS;
        } else {
            throw new HavalCommonException(MsgCodeEnum.QUERY_FAIL);
        }
    }

    /**
     * 根据角色code查找角色
     * @param roleCode 角色code
     * @return
     * @throws HavalCommonException
     */
    @Override
    public Long selectRole(String roleCode) throws HavalCommonException{
        log.info("正在查询角色信息...");
        RoleQuery roleQuery = new RoleQuery();
        roleQuery.createCriteria().andRoleCodeEqualTo(roleCode).andStatusEqualTo(RoleStatusEnum.NORMAL.getCode());
        List<RoleDO> roleList = roleManager.selectByQuery(roleQuery);
        log.info("查询角色信息完成...");
        if (!roleList.isEmpty()){
            return roleList.get(0).getRoleId();
        }
        throw new HavalCommonException(MsgCodeEnum.QUERY_FAIL);
    }

}
